package com.jsgt.greekfire.ui.wallet

import android.annotation.SuppressLint
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.base.BaseFragment
import com.fhx.core.goToActivity
import com.fhx.core.utils.LogUtils
import com.jsgt.greekfire.App
import com.jsgt.greekfire.ENTRUST_NUM
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.AtrAccountInfoBean
import com.jsgt.greekfire.common.util.UtilsBigDecimal
import com.jsgt.greekfire.dao.model.IdentityWallet
import com.jsgt.greekfire.dao.viewmodel.WalletViewModel
import com.jsgt.greekfire.ui.dialog.SafetyDialog
import com.jsgt.greekfire.ui.dialog.SwitchWalletDialog
import com.jsgt.greekfire.ui.wallet.assets.AssetsActivity
import com.jsgt.greekfire.ui.wallet.assets.ReceiveActivity
import com.jsgt.greekfire.viewmodel.UserViewModel
import kotlinx.android.synthetic.main.fragment_wallet.*

private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

class WalletFragment : BaseFragment(), View.OnClickListener {
    private var param1: String? = null
    private var param2: String? = null
    private var wallet: IdentityWallet? = null
    private var asset: AtrAccountInfoBean? = null

    private val walletViewModel: WalletViewModel by lazy {
        ViewModelProvider(this).get(WalletViewModel::class.java)
    }
    private val userViewModel: UserViewModel by lazy {
        ViewModelProvider(App.context).get(UserViewModel::class.java)
    }

    private val switchWalletDialog: SwitchWalletDialog by lazy {
        SwitchWalletDialog().selectListener {

        }
    }
    private val safetyDialog: SafetyDialog by lazy {
        SafetyDialog().okListener {
            goToActivity<WalletIdentityManagerActivity>() {
                this.putExtra("wallet", wallet)
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    companion object {
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            WalletFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_wallet
    }

    @SuppressLint("SetTextI18n")
    override fun main() {
        imvDetail.setOnClickListener(this)
        txvAddress.setOnClickListener(this)
        itemATR.setOnClickListener(this)
        imvSwitch.setOnClickListener(this)
        userViewModel.getAccountLiveData.observe(this, Observer {
            asset = it
            if (it != null) {
                if (!TextUtils.isEmpty(it.balance)) {
                    val div = UtilsBigDecimal.div(it.balance!!, ENTRUST_NUM)
                    tvBalance.text = div.toString()
                    tvTitleBalance.text = div.toString()
                } else {
                    tvBalance.text = "0.00"
                    tvTitleBalance.text = "0.00"
                }
            }
        })
        walletViewModel.getSelectWallet()?.observe(this, Observer {
            if (it != null && it.isNotEmpty()) {
                wallet = it[0]
                txvName.text = wallet?.walletName
                txvAddress.text = wallet?.address
                tvBalance.text = "0.00"
                tvTitleBalance.text = "0.00"
                userViewModel.getAccount(wallet?.address!!, "true")
            }
        })
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.imvDetail -> {
                wallet?.let {
                    if (it.identityName.isNullOrEmpty()) {
                        goToActivity<WalletManagerImportActivity> {
                            this.putExtra("wallet", it)
                        }
                    } else {
                        goToActivity<WalletManagerActivity> {
                            this.putExtra("wallet", it)
                        }
                    }
                }
            }
            R.id.txvAddress -> {
                checkBackup {
                    goToActivity<ReceiveActivity> {
                        this.putExtra("wallet", wallet)
                    }
                }
            }
            R.id.itemATR -> {
                checkBackup {
                    goToActivity<AssetsActivity> {
                        putExtra("wallet", wallet)
                    }
                }
            }
            R.id.imvSwitch -> {
                switchWalletDialog.show(parentFragmentManager, "switchWallet")
            }
        }
    }

    private fun checkBackup(action: () -> Unit) {
        if (wallet != null) {
            if (wallet!!.isBackup) {
                action.invoke()
            } else {
                safetyDialog.show(parentFragmentManager, "safety")
            }
        }
    }

    override fun onResume() {
        super.onResume()
        LogUtils.error("WalletFragmentonResume")
        if (wallet != null && !TextUtils.isEmpty(wallet?.address)) {
            userViewModel.getAccount(wallet?.address!!, "true")
        }
    }

}